package cn.wywk.demo.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import lombok.SneakyThrows;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * ========================
 *
 *
 * ========================
 */
public class ExcelUtil {

    /**
     * 单页导出excel
     * @param fileName 文件名
     * @param sheetName sheet页名
     * @param clazz 导出类
     * @param dataList 数据集合
     * @param response http响应
     * @param <T> 泛型
     */
    @SneakyThrows
    public static <T> void export(String fileName, String sheetName,Class<T> clazz,List<T> dataList, HttpServletResponse response){
        String urlFileName = URLEncoder.encode(fileName, "utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + urlFileName);
        EasyExcel.write(response.getOutputStream(),clazz).sheet(sheetName)
                .doWrite(dataList);
    }

    /**
     * 分页导出excel
     * @param fileName 文件名
     * @param sheetName sheet页名
     * @param clazz 导出类
     * @param dataList 数据集合
     * @param pageSize 每页大小
     * @param response http响应
     * @param <T> 泛型
     */
    @SneakyThrows
    public static <T> void pageExport(String fileName, String sheetName,Class<T> clazz,List<T> dataList,Integer pageSize,HttpServletResponse response){
        String urlFileName = URLEncoder.encode(fileName, "utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + urlFileName);
        //分页导出
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        int count = dataList.size();
        int batchCount = pageSize;
        int pages = (count % pageSize) == 0 ? count / pageSize : (count / pageSize + 1);
        int startIndex = 0;
        int endIndex = 0;
        WriteSheet writeSheet;
        if(count<=pageSize){
            writeSheet = EasyExcel.writerSheet(0, sheetName+"(1)").head(clazz).build();
            excelWriter.write(dataList, writeSheet);
        }else{
            for(int i=0;i<pages;i++){
                endIndex = (i== pages-1) ? count : endIndex + batchCount;
                writeSheet = EasyExcel.writerSheet(i, sheetName+"("+i+")").head(clazz).build();
                excelWriter.write(dataList.subList(startIndex, endIndex), writeSheet);
                startIndex = endIndex;
            }
        }
        excelWriter.finish();
    }
    @SneakyThrows
    public static <T> void pageExport(String fileName, String sheetName,List<List<String>> head ,List<T> dataList,Integer pageSize,HttpServletResponse response){
        String urlFileName = URLEncoder.encode(fileName, "utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + urlFileName);
        //分页导出
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        int count = dataList.size();
        int batchCount = pageSize;
        int pages = (count % pageSize) == 0 ? count / pageSize : (count / pageSize + 1);
        int startIndex = 0;
        int endIndex = 0;
        WriteSheet writeSheet;
        if(count<=pageSize){
            writeSheet = EasyExcel.writerSheet(0, sheetName+"(1)").head(head)
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(30)).build();
            excelWriter.write(dataList, writeSheet);
        }else{
            for(int i=0;i<pages;i++){
                endIndex = (i== pages-1) ? count : endIndex + batchCount;
                writeSheet = EasyExcel.writerSheet(i, sheetName+"("+i+")").head(head)
                        .registerWriteHandler(new SimpleColumnWidthStyleStrategy(30)).build();
                excelWriter.write(dataList.subList(startIndex, endIndex), writeSheet);
                startIndex = endIndex;
            }
        }
        excelWriter.finish();
    }



}
